//
//  IconTextCell.m
//  iLog fitness
//
//  Created by Keith Duncan on 04/05/2007.
//  Copyright 2007 BUOY. All rights reserved.
//

#import "ImageTextCell.h"

#import "Amber/Amber.h"

@implementation ImageTextCell

@synthesize image=_image;

- (void)dealloc {
	[_image release];
	
	[super dealloc];
}

- (NSRect)imageRectForBounds:(NSRect)frame {
	NSRect imageFrame = frame;
	imageFrame.size.width = NSHeight(frame);
	
	return imageFrame;
}

- (NSRect)titleRectForBounds:(NSRect)frame {
	if (_image == nil) return frame;
	NSRect imageFrame = [self imageRectForBounds:frame];
	
	NSRect textFrame = frame;
	textFrame.origin.x = NSMaxX(imageFrame);
	textFrame.size.width = NSWidth(frame) - NSWidth(imageFrame);
	
	return textFrame;
}

- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view {
	if (_image != nil) {
		NSRect imageFrame = [self imageRectForBounds:frame];
		
		BOOL imageFlipped = [_image isFlipped];
		[_image setFlipped:YES];
		
		[NSGraphicsContext saveGraphicsState];
		
		[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
		[_image drawInRect:SquareCenteredInRect((NSHeight(frame) * (3.0/4.0)), imageFrame) fromRect:(NSRect){.origin = NSZeroPoint, .size = [_image size]} operation:NSCompositeSourceOver fraction:1.0];
		
		[_image setFlipped:imageFlipped];
		
		[NSGraphicsContext restoreGraphicsState];
	}
	
	[self setTextColor:([self isHighlighted] ? [NSColor whiteColor] : [NSColor blackColor])];
	[self setFont:([self isHighlighted] ? [NSFont boldSystemFontOfSize:11] : [NSFont systemFontOfSize:11])];
	[self setShadowColor:([self isHighlighted] ? [NSColor darkGrayColor] : nil)];
	
	[self drawInteriorWithFrame:[self titleRectForBounds:frame] inView:view];
}

@end
